<?php

require_once plugin_dir_path( __FILE__ ) . '/filepaths.php' ;
require_once DPEM_LIB_PATH . '/smartmetabox/smartmetabox.php';

new events_creator();

class events_creator {
    private $metaboxes = array();

    public function __construct(){
        $this->metaboxes = array();

        //Hook calls
        add_action( 'init', array( &$this, 'create_events' ) );
        add_action( 'admin_head', array( &$this, 'remove_meta_box_base' ) );
        add_action( 'manage_posts_custom_column', array( &$this, 'events_custom_columns' ) );

        add_filter( 'manage_edit-event_columns', array( &$this, 'events_columns' ) );
        add_filter( 'the_content', array( &$this, 'my_editor_content' ) );

        $this->create_meta_fields();
    }

    public function create_events() {
      $labels = array(
        'name' => _x('Events', 'post type general name'),
        'singular_name' => _x('Events', 'post type singular name'),
        'add_new' => _x('Add New', 'Events'),
        'add_new_item' => __('Add New Event'),
        'edit_item' => __('Edit Event'),
        'new_item' => __('New Event'),
        'view_item' => __('View Event'),
        'search_items' => __('Search Events'),
        'not_found' =>  __('No Events found'),
        'not_found_in_trash' => __('No Events found in Trash'),
        'parent_item_colon' => ''
      );

      $supports = array('title', 'editor', 'author', 'thumbnail', 'revisions', 'excerpt', 'comments', 'custom-fields');

      $taxonomies = array('Events', 'Featured');

      register_post_type( 'event',
        array(
          'labels' => $labels,
          'public' => true,
          'supports' => $supports
        ) );
    }

    public function create_meta_fields(){
        //The order of the elements here will matter if you want to make the form.
        $this->metaboxes[]= add_smart_meta_box('eventlocation', array(
            'title' => __('Event\'s Location'),
            'pages'     => array('event'),
            'context'   => 'normal',
            'priority'  => 'high',
            'validate'  => TRUE,           //Flag: This metabox must be validated before sending the info.
            'fields'    => array(
                array(
                    'name' => __( 'Where: ' ),
                    'id' => 'dp_em_localization',
                    'desc' => __( '' ),
                    'default' => __( 'To be announced' ),
                    'type' => 'text',
                    'required' => TRUE,   //Flag: The field must have a value
                ),
                array(
                    'name' => __(''),
                    'id' => 'dp_em_metabox_message',
                    'desc' => __('Fields with ** are Mandatory'),
                    'type' => 'label'
                ))
        ));

        $this->metaboxes[] = add_smart_meta_box('eventschedule', array(
            'title'     => __('Event\'s Schedule'),
            'pages'     => array('event'),
            'context'   => 'normal',
            'priority'  => 'high',
            'validate'  => TRUE,           //Flag: This metabox must be validated before sending the info.
            'fields'    => array(
                array(
                    'name' => __( 'Start Date: ' ),
                    'id' => 'dp_em_startdate',
                    'desc' => __( '' ),
                    'type' => 'datepicker',
                    'readonly' => TRUE,
                    'default' => date('d/m/Y'), //Recommended if you are going to make the field required.
                    'required' => TRUE,         //Flag: The field must have a value
                    'comparable' => TRUE,       //Flag: If the object should be compared with any other of it's peers
                    'order' => 1                //Flag: The order of the object. Allows multicompares betwwen more than 2 objects.
                ),
                array(
                    'name' => __('Start Time: '),
                    'id' => 'dp_em_starttime',
                    'desc' => __( '' ),
                    'type' => 'timepicker',
                    'readonly' => TRUE,
                    'required' => FALSE,
                    'comparable' => TRUE,
                    'order' => 1
                ),
                array(
                    'type' => 'changeline',
                ),
                array(
                    'name' => __( 'End Date: ' ),
                    'id' => 'dp_em_enddate',
                    'desc' => __( '' ),
                    'type' => 'datepicker',
                    'readonly' => TRUE,
                    'default' => date('d/m/Y'),
                    'required' => TRUE,
                    'comparable' => TRUE,
                    'order' => 2
                ),
                array(
                    'name' => __('End Time: '),
                    'id' => 'dp_em_endtime',
                    'desc' => __( '' ),
                    'type' => 'timepicker',
                    'readonly' => TRUE,
                    'required' => FALSE,
                    'comparable' => TRUE,
                    'order' => 2
                ),
                array(
                    'type' => 'changeline',
                ),
                array(
                    'name' => __(''),
                    'id' => 'dp_em_metabox_message',
                    'desc' => __('Fields with ** are Mandatory'),
                    'type' => 'label'
                ),
            )
        ));
    }

    public function events_columns($columns) {
	$columns = array(
		'cb' => '<input type=checkbox />',
		'title' => __( 'Events' ),
                'localization' => __( 'Where?' ),
		'startdate' => __( 'Start Date' ),
                'enddate' => __( 'End Date' ),
		'comments' => __( 'Comments' )
	);
	return $columns;
    }

    public function events_custom_columns($column)
    {
	global $post;
	if ("ID" == $column) echo $post->ID;
	elseif ('localization' == $column) echo get_post_meta($post->ID, 'dp_em_localization',true);
        elseif ('startdate' == $column) echo get_post_meta($post->ID, 'dp_em_startdate',true);
	elseif ('enddate' == $column) echo get_post_meta($post->ID, 'dp_em_enddate',true);
        elseif ('comments' == $column) echo $post->comments;
    }

    function my_editor_content( $old_content ) {
            global $post;
            if ($post->post_type == 'event'){
                $local = "<li>Where?: " . get_post_meta($post->ID, 'dp_em_localization', true) . "</li>";
                $start = "<li>Start Date: " . get_post_meta($post->ID, 'dp_em_startdate', true) . ' @ ' . get_post_meta( $post->ID, 'dp_em_starttime', true ) . "</li>";
                $end = "<li>End Date: " . get_post_meta($post->ID, 'dp_em_enddate', true) . ' @ ' . get_post_meta( $post->ID, 'dp_em_endtime', true ) . "</li>";

                $end_message = "If you like this post, then please consider retweeting it or sharing it on Facebook.";

                $content = $local . $start . $end . $old_content . $end_message;
            } else {
                $content = $old_content;
            }
            return $content;
    }

    function remove_meta_box_base(){
         remove_meta_box( 'postcustom' , 'event' , 'normal');
    }

}



















/*
add_smart_meta_box('smart_meta_box_demo', array(
    'title'     => 'Smart meta box',
    'pages'     => array('event'),
    'context'   => 'normal',
    'priority'  => 'high',
    'fields'    => array(
        array(
            'name' => 'Text',
            'id' => 'smb_text',
            'default' => 'default',
            'desc' => 'Description',
            'type' => 'text',
        ),
        array(
            'name' => 'Textarea',
            'id' => 'smb_textarea',
            'default' => 'default',
            'desc' => 'Description',
            'type' => 'textarea',
        ),
        array(
            'name' => 'Checkbox',
            'id' => 'smb_checkbox',
            'default' => 'true',
            'desc' => 'Description',
            'type' => 'checkbox',
        ),
        array(
            'name' => 'Select',
            'id' => 'smb_select',
            'default' => 'second-val',
            'desc' => 'Description',
            'type' => 'select',
            'options' => array(
                'first-val' => 'First',
                'second-val' => 'Second',
            )
        ),
        array(
            'name' => 'Radios',
            'id' => 'smb_radio',
            'default' => 'second-val',
            'desc' => 'Description',
            'type' => 'radio',
            'options' => array(
                'first-val' => 'First',
                'second-val' => 'Second',
            )
        ),
    )
));
*/


?>
